package com.tancire.web.controller.app;

import com.tancire.common.annotation.Log;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.domain.model.LoginUser;
import com.tancire.common.enums.BusinessType;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.common.utils.StringUtils;
import com.tancire.file.bean.SaveFileBean;
import com.tancire.file.service.ISysFileService;
import com.tancire.net.domain.NetUser;
import com.tancire.net.service.INetUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 会员管理Controller
 *
 * @author chenchao
 * @date 2022-11-06
 */
@RestController
@RequestMapping("/net/app/user")
public class NetUserAppController extends BaseController {

    @Resource
    private INetUserService netUserService;

    @Resource
    private ISysFileService sysFileService;

    /**
     * 获取会员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        NetUser netUser = netUserService.getUserInfoById(loginUser.getUserId());
        return AjaxResult.success("success", netUser);
    }

    /**
     * 修改会员管理
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody NetUser netUser) {
        return toAjax(netUserService.updateNetUser(netUser));
    }

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("userId") String userId) {
        try {
            SaveFileBean saveFileBean = sysFileService.uploadFile(file, "N");
            // 查询会员信息
            if (StringUtils.isNotBlank(userId)) {
                this.netUserService.updateNetUserAvatar(userId, saveFileBean.getFileName());
            }
            return AjaxResult.success(saveFileBean);
        } catch (Exception e) {
            return AjaxResult.error("上传文件异常:" + e.getMessage());
        }
    }

    /**
     * 绑定邀请码
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @Log(title = "绑定邀请码", businessType = BusinessType.UPDATE)
    @GetMapping("/bind/invite/code/{code}")
    public AjaxResult bindInviteCode(@PathVariable("code") String code) {
        return toAjax(netUserService.bindInviteCode(code));
    }


    /**
     * 获取会员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @GetMapping(value = "/mine/profit")
    public AjaxResult getMineProfit() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        NetUser netUser = netUserService.getUserInfoById(loginUser.getUserId());
        return AjaxResult.success("success", netUser);
    }

    /**
     * 获取会员管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @GetMapping(value = "/mine/base/info")
    public AjaxResult getMineBaseInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return AjaxResult.success("success", null);
    }
}
